package com.yuju.util;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xuehj on 2017/10/13 0013.
 * 简易缓存
 */
public class SimpleCache<K, V> {
    private static SimpleCache instance = new SimpleCache();
    private SimpleCache() {}
    // 全局缓存
    private static Map<Object, Object> cache = new HashMap<Object, Object>();

    public static SimpleCache getInstance() {
        return instance;
    }

    /**
     * 添加缓存
     * @param key 缓存Key
     * @param obj 缓存对象
     */
    public void addCache(K key, V obj) {
        cache.put(key, obj);
    }

    /**
     * 获取缓存对象
     * @param key 缓存Key
     * @return 缓存对象
     */
    public V get(K key) {
        return (V) cache.get(key);
    }

    public static void main(String[] args) {
        SimpleCache<Integer, String> sc = SimpleCache.getInstance();
        sc.addCache(1,"Hello World!");
        System.out.println("我是泛型1=" + sc.get(1));

        SimpleCache<String, Integer> sc2 = SimpleCache.getInstance();
        sc2.addCache("test", 1);
        System.out.println("我是泛型2=" + sc2.get("test"));
    }
}
